package com.jml.队列和线程池;

import org.junit.Test;

import java.io.InputStream;
import java.util.concurrent.*;

public class ThreadPoolExecutorTest {

    @Test
    public void newCachedThreadPoolTest(){
        // 无限大小线程池 jvm自动回收
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            //线程执行
            newCachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    /*try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*/
                    System.out.println(Thread.currentThread().getName() + ",i:" + temp);
                }
            });
        }
    }
    @Test
    public void newFixedThreadPoolTest() throws InterruptedException {
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            newFixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ",i:" + temp);
                }
            });
        }
        System.out.println("xx");
    }

    @Test
    public void newScheduledThreadPoolTest(){
        ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            newScheduledThreadPool.schedule(new Runnable() {
                public void run() {
                    System.out.println("i:" + temp);
                }
            }, 3, TimeUnit.SECONDS);
        }
    }
    @Test
    public void newSingleThreadExecutorlTest(){
        ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            newSingleThreadExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("index:" + index);
                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                    }
                }
            });
        }
    }

    @Test
    public void myThreadPoolTest(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3));
        executor.execute(new TaskThred("任务1"));
        //达到核心线程数，进入缓存队列
        executor.execute(new TaskThred("任务2"));
        executor.execute(new TaskThred("任务3"));
        executor.execute(new TaskThred("任务4"));
        //队列满了开始创建新的线程
        executor.execute(new TaskThred("任务5"));
        //达到最大线程数，此时会报错拒绝执行
       // executor.execute(new TaskThred("任务6"));
    }
    class TaskThred implements Runnable {
        private String taskName;
        public TaskThred(String taskName) {
            this.taskName = taskName;
        }
        @Override
        public void run() {
            System.out.println("begin:"+Thread.currentThread().getName()+taskName);
            System.out.println("end:"+Thread.currentThread().getName()+taskName);
        }

    }

}
